﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ITSP.SharePoint.CacheRefresh.Examples
{
    /// <summary>
    /// This Configuration Manager uses the IIS Cache to store the configuration for the specified time by the Cache Configuration List.
    /// </summary>
    public class BaseCacheConfigurationManager : BaseConfigurationManager
    {
        /// <summary>
        /// Constructor requires the Url that hosts the Cache Configuration List.
        /// Requires the key to use for getting the settings to caching the configuration and finally a key to store the object in the cache.
        /// </summary>
        /// <param name="cacheHostUrl">The Url for the Cache Configuration List</param>
        /// <param name="listCacheKey">The Identity of the Cache Configuration Item in the list</param>
        /// <param name="cacheKey">The Identity of the Cache Key used for the IIS Cache.</param>
        public BaseCacheConfigurationManager(string cacheHostUrl, string listCacheKey, string cacheKey)
            :base(cacheHostUrl,listCacheKey,cacheKey,"")
        {
        }

        /// <summary>
        /// Load function which gets the object from the IIS cache.
        /// </summary>
        /// <typeparam name="T">The Type object</typeparam>
        /// <returns>The object of Type T if found, otherwise null.</returns>
        public override T Load<T>()
        {
            return GetFromCache<T>();
        }

        /// <summary>
        /// Save function that saves the object to the IIS Cache.
        /// </summary>
        /// <typeparam name="T">The Type object</typeparam>
        /// <param name="configuration">the configuratino object of type T</param>
        /// <returns>true if successful else false</returns>
        public override bool Save<T>(T configuration)
        {
            return AddToCache(configuration);
        }

        /// <summary>
        /// Load function gets the object from the IIS cache
        /// This provides a method to specify the Cache Key to use.
        /// </summary>
        /// <typeparam name="T">Type of object to load</typeparam>
        /// <param name="cacheKey">The Cache Key that was used to store the object in the cache.</param>
        /// <returns>The object if found or null.</returns>
        public virtual T Load<T>(string cacheKey)
        {
            return GetFromCache<T>(cacheKey);
        }

        /// <summary>
        /// Save function saves to the IIS Cache.
        /// This provides a method to specify the Cache Key to use.
        /// </summary>
        /// <typeparam name="T">The Type object</typeparam>
        /// <param name="configuration">the configuratino object of type T</param>
        /// <param name="cacheKey">The Cache key that should be used to retrive the object from the Cache with.</param>
        /// <returns>true if successful else false</returns>
        public virtual bool Save<T>(T configuration, string cacheKey)
        {
            return AddToCache(configuration, cacheKey);
        }
    }
}
